package com.geek.framework.study.config.microprofile.source;

import javax.servlet.ServletContext;
import java.util.Enumeration;
import java.util.Map;

import static com.geek.framework.study.config.microprofile.enums.ConfigSourceEnum.SERVLET_CONTEXT_CONFIG;


public class ServletContextConfigSource extends MapBaseConfigSource{
    private final ServletContext servletContext;

    public ServletContextConfigSource(ServletContext servletContext) {
        super(SERVLET_CONTEXT_CONFIG.getName(), SERVLET_CONTEXT_CONFIG.getOrdinal());
        this.servletContext = servletContext;
    }

    @Override
    protected void prepareConfigData(Map<String, String> configData) {
        Enumeration<String> parameterNames = servletContext.getInitParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            configData.put(parameterName, servletContext.getInitParameter(parameterName));
        }
    }
}
